package com.syj.collect.model;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.syj.collect.Present.LoginPresent;
import com.syj.collect.app.MainActivity;
import com.syj.collect.app.MyApplication;
import com.syj.collect.app.R;
import com.syj.collect.base.BaseMvpAvtivity;

import com.syj.collect.utils.SPUtil;
import com.syj.collect.view.LoginView;

import butterknife.internal.Utils;

public class LoginActivity extends BaseMvpAvtivity<LoginView, LoginPresent> implements LoginView {
    private EditText userName, pwd;
    private  boolean ispwd;
    private ImageView iv_xianshi;
    private CheckBox saveChb;
    @Override
    public LoginPresent initPresenter() {
        return new LoginPresent();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


//        if ("" != SPUtil.getPrefString("token", "") && ((MyApplication) getApplication()).getIslogin())
//            StartAction();

        setContentView(R.layout.activity_login);
        userName = (EditText) findViewById(R.id.login_user);
        pwd = (EditText) findViewById(R.id.login_pwd);
        iv_xianshi = findViewById(R.id.iv_xianshi);
        saveChb = findViewById(R.id.saveChb);

        SharedPreferences sp2=getSharedPreferences("Logindb",MODE_PRIVATE);
        if(sp2.getBoolean("save",false)==true  ){    //判断是否写入了数值save==true
            getDB();
        }

        pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               /* ((MyApplication) getApplication()).setIslogin(true);
                StartAction();*/
                checkLogin();
                //LoginActivity.this.finish();
            }
        });

        iv_xianshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ispwd=!ispwd;
                if (ispwd){
                    pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    iv_xianshi.setBackgroundResource(R.drawable.open);
                }else {
                    pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    iv_xianshi.setBackgroundResource(R.drawable.biyan);
                }
                pwd.setSelection(pwd.getText().toString().trim().length());
            }
        });

    }

    private void checkLogin() {
        if (isEmpty(userName.getText().toString())) {

        } else if (isEmpty(pwd.getText().toString())) {
        } else {
            presenter.Login(userName.getText().toString(), pwd.getText().toString());
        }


    }

    @Override
    public void StartAction() {
        Intent intent = new Intent(this, MainActivity.class);
        this.startActivity(intent);
        this.finish();

    }

    @Override
    public void initSuccess(String str) {
        Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
        ((MyApplication) getApplication()).setIslogin(true);
        if (saveChb.isChecked()){
            saveDB();
        }else {
            clearDB();
        }
        StartAction();
    }

    @Override
    public void initFail(String str) {
        ((MyApplication) getApplication()).setIslogin(false);
        Toast.makeText(this, str, Toast.LENGTH_LONG).show();
    }

    public boolean isEmpty(String str) {
        if (str != null && str != "" && !str.isEmpty()) {
            return false;
        }
        return true;
    }
    //保存数据
    private void saveDB(){
        SharedPreferences sp=getSharedPreferences("Logindb",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("loginEdt",userName.getText().toString());
        editor.putString("passwordEdt",pwd.getText().toString());
        editor.putBoolean("save",true);
        editor.commit();            //写入数据
//        Toast.makeText(LoginActivity.this,"sd",Toast.LENGTH_LONG).show();
    }
    //读取数据
    private void getDB() {
        SharedPreferences sp = getSharedPreferences("Logindb", MODE_PRIVATE);
        String name = sp.getString("loginEdt", "");
        String password = sp.getString("passwordEdt", "");
        if (!name.isEmpty()){
            userName.setText(name);
        }
        if (!password.isEmpty())
        pwd.setText(password);
    }
    //清除
    private void clearDB(){
        SharedPreferences sp=getSharedPreferences("Logindb",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.clear();
        editor.commit();
    }

}
